package com.kim.rssreader.service;

import com.kim.rssreader.db.DBDAO;
import com.kim.rssreader.utils.NetUtil;
import com.kim.rssreader.utils.RssColumnUpdateUtils;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

/**
 * rss栏目更新服务
 * 
 * @author Kimhuang
 * 
 */
public class RssColumnUpdateService extends Service {

	/**
	 * 启动action
	 */
	public static final String ACTION_FETCH = "com.kim.rssreader.rsscolumnupdate";

	/**
	 * 栏目配置更新 数据抓取完毕
	 * 
	 * @author Kimhuang
	 * 
	 */
	public interface RssColumnConfCallback {
		void onError();

		void onComplete();
	}

	/**
	 * 栏目数据更新 数据抓取完毕
	 * 
	 * @author Kimhuang
	 * 
	 */
	public interface RssColumnDataCallback {
		void onError();

		void onComplete();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (NetUtil.isNetAvailable(this)) {
			final RssColumnDataCallback rssColumnDataCallback = new RssColumnDataCallback() {

				@Override
				public void onError() {
					// 抓取失败，关闭服务
					stopSelf();
				}

				@Override
				public void onComplete() {
					// 抓取成功，关闭服务
					stopSelf();
				}
			};
			RssColumnConfCallback rssColumnConfCallback = new RssColumnConfCallback() {

				@Override
				public void onError() {
					// 抓取失败，关闭服务
					stopSelf();
				}

				@Override
				public void onComplete() {
					// 抓取成功，下载栏目数据
					RssColumnUpdateUtils.fetchRssColumnData(
							getApplicationContext(), rssColumnDataCallback);
				}
			};
			RssColumnUpdateUtils.updateRssColumn(this, rssColumnConfCallback);
		} else {
			stopSelf();
		}
		return super.onStartCommand(intent, flags, startId);
	}

}
